package com.yang.test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.zip.ZipInputStream;

import org.jbpm.api.Configuration;
import org.jbpm.api.ProcessDefinition;
import org.jbpm.api.ProcessDefinitionQuery;
import org.jbpm.api.ProcessEngine;
import org.junit.Test;

public class MyProcessDefinition {

	private ProcessEngine processEngine = Configuration.getProcessEngine();

	@Test
	public void testDeploy_01() {
		processEngine.getRepositoryService()//
				.createDeployment()//
				.addResourceFromClasspath("test/test.png")//
				.addResourceFromClasspath("test/test.jpdl.xml")//
				.deploy();
	}

	@Test
	public void testDeploy_02() {
		InputStream in = this.getClass().getClassLoader()
				.getResourceAsStream("helloword/helloword.zip");

		ZipInputStream zipInputStream = new ZipInputStream(in);

		processEngine.getRepositoryService()//
				.createDeployment()//
				.addResourcesFromZipInputStream(zipInputStream)//
				.deploy();
	}

	@Test
	public void testFindAll() {
		List<ProcessDefinition> list = processEngine.getRepositoryService()//
				.createProcessDefinitionQuery()//
				.list();

		for (ProcessDefinition pd : list) {
			System.out.println("id=" + pd.getId()//
					+ ", name=" + pd.getName()//
					+ ", key=" + pd.getKey()//
					+ ", version=" + pd.getVersion()//
					+ ", deploymentId=" + pd.getDeploymentId());
		}
	}

	@Test
	public void testFindAllLatestVersion() {
		List<ProcessDefinition> allList = processEngine.getRepositoryService()//
				.createProcessDefinitionQuery()//
				.orderAsc(ProcessDefinitionQuery.PROPERTY_VERSION)//
				.list();

		Map<String, ProcessDefinition> map = new HashMap<String, ProcessDefinition>();
		for (ProcessDefinition pd : allList) {
			map.put(pd.getKey(), pd);
		}

		for (ProcessDefinition pd : map.values()) {
			System.out.println("id=" + pd.getId()//
					+ ", name=" + pd.getName()//
					+ ", key=" + pd.getKey()//
					+ ", version=" + pd.getVersion()//
					+ ", deploymentId=" + pd.getDeploymentId());
		}
	}

	@Test
	public void testDeleteByDeploymentId() {
		String deploymentId = "50001";
		processEngine.getRepositoryService().deleteDeploymentCascade(
				deploymentId);
	}

	@Test
	public void testDeleteByDeploymentKey() {
		String key = "helloword";
		List<ProcessDefinition> list = processEngine.getRepositoryService()//
				.createProcessDefinitionQuery()//
				.processDefinitionKey(key)//
				.list();

		for(ProcessDefinition pd : list) {
			processEngine.getRepositoryService().deleteDeploymentCascade(
					pd.getDeploymentId());
		}
	}

	@Test
	public void testGetResource() throws IOException {
		String deploymentId = "60001";
		String resourceName = "helloword.png";

		Set<String> names = processEngine.getRepositoryService()
				.getResourceNames(deploymentId);
		for (String name : names) {
			System.out.println(name);
		}

		FileOutputStream out = new FileOutputStream("c:/1.png");
		InputStream in = processEngine.getRepositoryService()
				.getResourceAsStream(deploymentId, resourceName);
		for (int b = -1; (b - in.read()) != -1;) {
			out.write(b);
		}
		in.close();
		out.close();
	}
}
